package forms;

import classes.Conexao;
import classes.Utilitarios;
import javax.swing.JOptionPane;

/**
 *
 * @author Yanni
 */
public class FormAlterarComanda extends javax.swing.JInternalFrame {

    Utilitarios u = new Utilitarios();
    /**
     * Creates new form FormAlterarComanda
     */
    public FormAlterarComanda()
    {
        initComponents();    
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ativaButtonGroup = new javax.swing.ButtonGroup();
        ativaPanel = new javax.swing.JPanel();
        rbtNao = new javax.swing.JRadioButton();
        rbtSim = new javax.swing.JRadioButton();
        btnAlterarComanda = new javax.swing.JButton();
        lblCdComanda = new javax.swing.JLabel();
        txtCdComanda = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setTitle("Alterar Comanda");

        ativaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Ativa?"));
        ativaPanel.setToolTipText("Comanda ativa ou inativa?");

        ativaButtonGroup.add(rbtNao);
        rbtNao.setText("Não");
        rbtNao.setToolTipText("Selecione esta opção para desativar a comanda");

        ativaButtonGroup.add(rbtSim);
        rbtSim.setText("Sim");
        rbtSim.setToolTipText("Selecione esta opção para ativar a comanda");

        javax.swing.GroupLayout ativaPanelLayout = new javax.swing.GroupLayout(ativaPanel);
        ativaPanel.setLayout(ativaPanelLayout);
        ativaPanelLayout.setHorizontalGroup(
            ativaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ativaPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ativaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtNao)
                    .addComponent(rbtSim))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        ativaPanelLayout.setVerticalGroup(
            ativaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ativaPanelLayout.createSequentialGroup()
                .addComponent(rbtSim)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbtNao))
        );

        btnAlterarComanda.setText("Alterar");
        btnAlterarComanda.setToolTipText("Clique aqui para gravar as alterações da comanda");
        btnAlterarComanda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarComandaActionPerformed(evt);
            }
        });

        lblCdComanda.setText("Código:");

        txtCdComanda.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblCdComanda)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCdComanda, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(btnAlterarComanda, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(ativaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCdComanda)
                    .addComponent(txtCdComanda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ativaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAlterarComanda)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-222)/2, (screenSize.height-228)/2, 222, 228);
    }// </editor-fold>//GEN-END:initComponents

    private void btnAlterarComandaActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnAlterarComandaActionPerformed
    {//GEN-HEADEREND:event_btnAlterarComandaActionPerformed
        try
        {
            int cod = Integer.parseInt(txtCdComanda.getText());
            char ativa = ' ';
            
            if(rbtSim.isSelected()) {
                ativa = 'S';
            }
            else if(rbtNao.isSelected()) {
                ativa = 'N';
            }
            
            String sql = "UPDATE comanda SET \"ic_ativa_inativa\" = '" + ativa + "' WHERE \"cd_comanda\" = " + cod;
                        
            if(Conexao.atualizar(sql) == -1)
            {
                JOptionPane.showMessageDialog(null, "O registro não pode ser alterado:\n" + Conexao.getErro(), "Erro", 0);
                this.dispose();
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Registro alterado com sucesso.", "Aviso", 1);
                this.dispose();
            }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Erro: \n" + e.getMessage(), "Erro!", 0);
            this.dispose();
        }
    }//GEN-LAST:event_btnAlterarComandaActionPerformed

    public void passarValoresComanda(int cod, String ativa) 
    {
        txtCdComanda.setText(Integer.toString(cod));
        
        if(ativa.equalsIgnoreCase("ativa")) {
            rbtSim.setSelected(true);
        }
        else if(ativa.equalsIgnoreCase("inativa")) {
            rbtNao.setSelected(true);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup ativaButtonGroup;
    private javax.swing.JPanel ativaPanel;
    private javax.swing.JButton btnAlterarComanda;
    private javax.swing.JLabel lblCdComanda;
    private javax.swing.JRadioButton rbtNao;
    private javax.swing.JRadioButton rbtSim;
    public javax.swing.JTextField txtCdComanda;
    // End of variables declaration//GEN-END:variables
}
